var socket = require("../lib/socket_connect.js");
var co = require("gq-core/node_modules/co");
var request = require("request");
module.exports = {
    "prefix": "/admin/download/excelfile",
    "get": {
        "/finance/:name": [{
            doc: {
                des: "财务统计Excel表格下载",
                params: [{
                    name: "name",
                    can_null: false,
                    des: "表名",
                    type: ""
                }, {
                    name: "query.type",
                    des: "类型字段"
                }]
            },
            emit_with: ["session", "params", "query"]
        }, function*(data) {
            var ExcelBuild = require("../class/excel_build.js");
            console.log(data.query.type);
            var excel_buffer = yield ExcelBuild.build_excel_file(yield socket.redisExec("GET", [data.params.name]), data.query.type);
            socket.handles.returnData(data.task_id, {
                response_type: "xlsx",
                body: excel_buffer,
            });
        }],
        "/finance_calculate": [{
            doc: {
                des: "处理报表数据",
                params: [{
                    name: "query.name",
                    can_null: false,
                    des: "表名",
                    type: ""
                }, {
                    name: "query.type",
                    des: "类型字段",
                    can_null: false
                }]
            },
            emit_with: ["session", "params", "query"]
        }, function*(data) {
            // console.log("data", data);
            var type = data.query.type;
            var datas = yield socket.redisExec("GET", [data.query.name]);
            var json = JSON.parse(datas);
            datas = json.filter(function(json_item) {
                return json_item.type == type;
            });
            datas = datas[0];
            var obj = {};
            if (datas) {
                if (type == "user-recharge") { //用户充值
                    console.log(datas.list.length)
                    obj = {
                        count: 0, //充值总笔数
                        total_recharge: 0 //充值总额
                    };
                    obj.count = datas.list.length;
                    datas.list.forEach(function(item, index) {
                        // console.log(item)
                        obj.total_recharge = obj.total_recharge + item.amount;
                    });
                };
                if (type == "user-create-user-with-membertype") { //创建新用户
                    var rebate_value = 0;
                    var rebate_assist = 0;

                    function _(cb) {
                        request.get("http://127.0.0.1:4100/admin/statistics/_get_rebates_chain_logs/" + datas.list[0].log_id + "/" + datas.list[datas.list.length - 1].log_id).on("response", function(response) {
                            var bufs = []
                            response.on("data", function(result) {
                                bufs.push(result)
                            });
                            response.on("end", function() {
                                var res = Buffer.concat(bufs);
                                res = JSON.parse(res.toString());
                                res.info.forEach(function(item, index) {
                                    rebate_assist += item.data.amounts.filter(function(a) {
                                        return a.key == "cache_assist";
                                    })[0].amount;
                                    rebate_value += item.data.amounts.filter(function(a) {
                                        return a.key == "cache_balance";
                                    })[0].amount;
                                });
                                cb();
                            });
                        });
                    };
                    yield _;
                    console.log(rebate_assist)
                    console.log(rebate_value)
                    obj = {
                        count: datas.list.length || 0, //新建用户数
                        level_a_count: 0, //新建天使车主数
                        level_b_count: 0, //财富车主数
                        level_c_count: 0, //豪金车主数
                        total_stock: 0, //新增会员股份
                        total_assist: rebate_assist, //新增会员积金
                        total_outgo: 0, //支出金额
                        total_income: 0, //统计新建用户进账
                        profit: 0, //利润
                        rebate_assist: rebate_assist,
                        rebate_value: rebate_value
                    };
                    datas.list.forEach(function(item, index) {
                        obj.total_assist = obj.total_assist + 10000;
                        // console.log(item.new_user_asset)
                        // if (item.new_user_asset.level == 1) {
                        if (item.new_user_asset.car_flag == "天使车主") {
                            obj.level_a_count = obj.level_a_count + 1;
                            // obj.total_income = obj.total_income + 10000;
                        };
                        // if (item.new_user_asset.level == 2) {
                        if (item.new_user_asset.car_flag == "财富车主") {
                            obj.level_b_count = obj.level_b_count + 1;
                            // obj.total_income = obj.total_income + 30000;
                        };
                        // if (item.new_user_asset.level == 3) {
                        if (item.new_user_asset.car_flag == "豪金车主") {
                            obj.level_c_count = obj.level_c_count + 1;
                            // obj.total_income = obj.total_income + 50000;
                        };
                        obj.total_income = obj.total_income + item.amount;
                        obj.total_stock = obj.total_stock + item.new_user_asset.stock;
                        item.new_user_asset.rebates_chain.forEach(function(new_item, index) {
                            obj.total_outgo = obj.total_outgo + new_item.rebate_value;
                        });
                    });
                    obj.profit = obj.total_income - obj.total_outgo;
                };
                if (type == "transfers-to-user") {
                    obj = {
                        count: datas.list.length || 0, // 转账笔数统计
                        total_amount: 0, // 转账金额总计
                        total_fee: 0 // 手续费统计
                    };
                    datas.list.forEach(function(item, index) {
                        obj.total_amount = obj.total_amount + item.amount;
                        obj.total_fee = obj.total_fee + item.fee;
                    });
                };
                if (type == "confirm-withdrawals-arrive") {
                    obj = {
                        count: datas.list.length || 0, //提现总笔数
                        total_amount: 0, //提现金额统计
                        total_fee: 0 //提现手续费统计
                    };
                    datas.list.forEach(function(item, index) {
                        obj.total_amount = obj.total_amount + item.amount;
                        obj.total_fee = obj.total_fee + item.fee;
                    });
                };
                if (type == "user-upgrade-asset-level") {
                    obj = {
                        count: datas.list.length || 0, //用户升级人数
                        level_to_b_count: 0, //升级到财富人数
                        level_to_c_count: 0 //升级到豪金人数
                    };
                    datas.list.forEach(function(item, index) {
                        if (item.new_data.level == 2) {
                            obj.level_to_b_count = obj.level_to_b_count + 1;
                        };
                        if (item.new_data.level == 3) {
                            obj.level_to_c_count = obj.level_to_c_count + 1;
                        };
                    });
                };
            };
            console.log(obj);
            this.body = obj;

        }]
    }
};